import * as StringUtils from './stringUtils'
import React from 'react'
//公用方法

// 模拟锚点
export const anchor = {
    created: function () {
    },
    methods: {
        anchor:function(e){
            let id='anchor-'+e;
            let anchor=document.getElementById(id);
            let go=anchor.offsetTop;
      
            // console.log(go)
            Math.animation(document.documentElement.scrollTop,go,800,'Quart.easeOut', function (value) {
                document.documentElement.scrollTop = value;
            });
          }
    }
}
//ip
// export const webUrl='http://39.108.70.164:8888/'
export const webUrl='https://www.yangyuetao.cn:8888/'

//visit统计
export const visitCount={
    beforeCreate:function(){
        this.$axios.post(webUrl+'api/visitCount',{'view':1})
          .then((response) => {
          })
          .catch((reject) => {
            console.log(reject)
          })
    }
}

const tableConfig = {
    dom: "<'container-fluid'<'row'<'col-7'<'toolbar'>><'col-5'f>>" +
                  "<'row'<'col-12'tr>>" +
                  "<'row'<'col-5'li><'col-7'p>>>",
    processing: true,
    serverSide: true,
    aLengthMenu: [5,10,20,50,100],
    order: [[0,"desc"]],
    language: {
        "sProcessing": "处理中...",
        "sLengthMenu": "显示 _MENU_ 项结果",
        "sZeroRecords": "没有匹配结果",
        "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
        "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
        "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
        "sInfoPostFix": "",
        "sSearch": "搜索:",
        "sUrl": "",
        "sEmptyTable": "表中数据为空",
        "sLoadingRecords": "载入中...",
        "sInfoThousands": ",",
        "oPaginate": {
            "sFirst": "首页",
            "sPrevious": "上页",
            "sNext": "下页",
            "sLast": "末页"
        },
        "oAria": {
            "sSortAscending": ": 以升序排列此列",
            "sSortDescending": ": 以降序排列此列"
        }
    }
}

export function initTable(uiConfig = {}, initTabConf = {}) {
    const toolbarId = uiConfig.toolbarId || '', tableId = uiConfig.tableId || ''
    if(StringUtils.isEmpty(toolbarId)) {
        return 
    }
    let myTabConfig = {}
    Object.assign(myTabConfig, tableConfig,initTabConf)
    $('#' + tableId).DataTable(myTabConfig)
    $('.buttons-copy, .buttons-csv, .buttons-print, .buttons-pdf, .buttons-excel, .buttons-add').addClass('btn btn-primary mr-1 btn-sm');

    let tableWrapperId = '#' + tableId + '_wrapper'
    if(StringUtils.isNotEmpty(toolbarId)) {
        const toolbarContent = $('#' + toolbarId)
        let  buttons = $(tableWrapperId + ' div.dt-buttons')
        buttons.children().each((arg0,arg1)=>{
            toolbarContent.append($(arg1))
        })
    }

    const toolbar = $(tableWrapperId + ' .toolbar')
    toolbar.append($('#' + toolbarId))

    // 初始化事件
    $('#' + tableId).on('draw.dt',(e,setting)=>{
        // 获取事件名称
        let columns = setting.aoColumns,dataTable = $('#' + tableId).DataTable()
        for(let i = 0; i < columns.length; i++) {
            if(columns[i]['events'] != null) {
                for(let key in columns[i]['events']) {
                    let keys = key.split(' '), evtName = keys[0].trim(), cls = keys[1].trim(),rows = dataTable.rows().nodes().to$(), data = dataTable.rows().data()
                    for(let j = 0; j < rows.length; j ++) {
                        // 根据事件名称及class绑定事件
                        $ (cls,rows[j] ).on( evtName ,(e)=>{
                            let value = null, fieldName = columns[i].data
                            if(fieldName != '' && fieldName && fieldName != undefined) {
                                value = data[j][fieldName]
                            }
                            // 调用自定义函数 
                            columns[i]['events'][key](e, value, data[j])
                        })
                    }
                }
            }
        }
    })
}

export function buttonAdd(per, url, pers = loginInfo.permissions) {
    if($.inArray(per, pers) > 0) {
        return (
            <a href={url} className="dt-button btn btn-primary waves-effect waves-light btn-sm buttons-html5 mr-1" title="添加">
                添加
            </a>
        )
    }
}

export function buttonEdit(per, pers = loginInfo.permissions) {
    if($.inArray(per, pers) < 0) {
        return ''
    }

    return '<a type="button" href="javascript:void(0);" class="btn btn-xs waves-effect waves-light btn-primary update">编辑</a>'
}

export function buttonDelete(per, pers = loginInfo.permissions) {
    if($.inArray(per, pers) < 0) {
        return ''
    }

    return '<a type="button" href="javascript:void(0);" class="btn btn-xs waves-effect waves-light btn-danger remove">删除</a>'
}

export function checkRight(per, pers = loginInfo.permissions) {
    if($.inArray(per, pers) < 0) {
        return false
    }

    return true
}